#!/usr/bin/env bash

# Test HTTP backend caching functionality
# This test verifies that the same tarball is reused when multiple tools use it

# First, let's check if the cache directory exists and is empty
CACHE_DIR="$HOME/.cache/mise/http-tarballs"
if [ -d "$CACHE_DIR" ]; then
	echo "Clearing existing HTTP cache..."
	rm -rf "$CACHE_DIR"
fi

# Test 1: Install a tool and verify cache is created
echo "Test 1: Installing first tool and checking cache creation"
cat <<EOF >mise.toml
[tools]
"http:hello-cache1" = { version = "1.0.0", url = "https://mise.jdx.dev/test-fixtures/hello-world-1.0.0.tar.gz", bin_path = "hello-world-1.0.0/bin", postinstall = "chmod +x \$MISE_TOOL_INSTALL_PATH/hello-world-1.0.0/bin/hello-world" }
EOF

mise install
assert_contains "mise x -- hello-world" "hello world"

# Check that cache directory was created
if [ ! -d "$CACHE_DIR" ]; then
	echo "ERROR: Cache directory was not created"
	exit 1
fi

# Count cache entries (should be 1)
CACHE_COUNT=$(find "$CACHE_DIR" -maxdepth 1 -type d | wc -l)
CACHE_COUNT=$((CACHE_COUNT - 1)) # Subtract 1 for the cache directory itself
echo "Cache entries after first install: $CACHE_COUNT"
if [ "$CACHE_COUNT" -ne 1 ]; then
	echo "ERROR: Expected 1 cache entry, found $CACHE_COUNT"
	exit 1
fi

# Test 2: Install another tool with the same tarball and verify cache is reused
echo "Test 2: Installing second tool with same tarball and checking cache reuse"
cat <<EOF >mise.toml
[tools]
"http:hello-cache2" = { version = "1.0.0", url = "https://mise.jdx.dev/test-fixtures/hello-world-1.0.0.tar.gz", bin_path = "hello-world-1.0.0/bin", postinstall = "chmod +x \$MISE_TOOL_INSTALL_PATH/hello-world-1.0.0/bin/hello-world" }
EOF

mise install
assert_contains "mise x -- hello-world" "hello world"

# Count cache entries (should still be 1)
CACHE_COUNT=$(find "$CACHE_DIR" -maxdepth 1 -type d | wc -l)
CACHE_COUNT=$((CACHE_COUNT - 1)) # Subtract 1 for the cache directory itself
echo "Cache entries after second install: $CACHE_COUNT"
if [ "$CACHE_COUNT" -ne 1 ]; then
	echo "ERROR: Expected 1 cache entry, found $CACHE_COUNT"
	exit 1
fi

# Test 3: Install a tool with a different tarball and verify new cache entry
echo "Test 3: Installing tool with different tarball and checking new cache entry"
cat <<EOF >mise.toml
[tools]
"http:hello-cache3" = { version = "2.0.0", url = "https://mise.jdx.dev/test-fixtures/hello-world-2.0.0.tar.gz", bin_path = "hello-world-2.0.0/bin", postinstall = "chmod +x \$MISE_TOOL_INSTALL_PATH/hello-world-2.0.0/bin/hello-world" }
EOF

mise install
assert_contains "mise x -- hello-world" "hello world 2.0.0"

# Count cache entries (should be 2)
CACHE_COUNT=$(find "$CACHE_DIR" -maxdepth 1 -type d | wc -l)
CACHE_COUNT=$((CACHE_COUNT - 1)) # Subtract 1 for the cache directory itself
echo "Cache entries after third install: $CACHE_COUNT"
if [ "$CACHE_COUNT" -ne 2 ]; then
	echo "ERROR: Expected 2 cache entries, found $CACHE_COUNT"
	exit 1
fi

# Test 4: Install a tool with checksum and verify cache key uses checksum
echo "Test 4: Installing tool with checksum and checking cache key"
cat <<EOF >mise.toml
[tools]
"http:hello-cache4" = { version = "1.0.0", url = "https://mise.jdx.dev/test-fixtures/hello-world-1.0.0.tar.gz", bin_path = "hello-world-1.0.0/bin", checksum = "sha256:dbca4f08377d70dc0828f5822fc47ecec3895cd9a6dacbc54cc984d346a571af", postinstall = "chmod +x \$MISE_TOOL_INSTALL_PATH/hello-world-1.0.0/bin/hello-world" }
EOF

mise install
assert_contains "mise x -- hello-world" "hello world"

# Count cache entries (should still be 2, as it should reuse the existing cache)
CACHE_COUNT=$(find "$CACHE_DIR" -maxdepth 1 -type d | wc -l)
CACHE_COUNT=$((CACHE_COUNT - 1)) # Subtract 1 for the cache directory itself
echo "Cache entries after fourth install: $CACHE_COUNT"
if [ "$CACHE_COUNT" -ne 2 ]; then
	echo "ERROR: Expected 2 cache entries, found $CACHE_COUNT"
	exit 1
fi

# Test 5: Verify that install directories are symlinks to cache
echo "Test 5: Verifying install directories are symlinks to cache"
INSTALL_DIR="$HOME/.local/share/mise/installs/http-hello-cache1/1.0.0"
if [ ! -L "$INSTALL_DIR" ]; then
	echo "ERROR: Install directory is not a symlink: $INSTALL_DIR"
	exit 1
fi

# Check that the symlink points to the cache
LINK_TARGET=$(readlink "$INSTALL_DIR")
if [[ ! $LINK_TARGET =~ /http-tarballs/ ]]; then
	echo "ERROR: Symlink does not point to cache directory: $LINK_TARGET"
	exit 1
fi

echo "SUCCESS: All HTTP backend caching tests passed!"

# Clean up
mise uninstall --all
